const Sequelize = require("sequelize")
const { Dict, DictDetail } = require("../model")
const moment = require("moment")
const utils = require("../utils/index.js")

const DictDetailHasMany = Dict.hasMany(DictDetail, {
  foreignKey: "dict_id",
  constraints: false,
  as: "data",
  order: ["sort", "ASC"]
})

const getAllDict = async store_id => {
  console.log("getAllDict--store_id: %s==", store_id)
  const data = await Dict.findAll({
    where: {
      store_id: {
        [Sequelize.Op.in]: ["0", store_id]
      }
    },
    include: [
      {
        association: DictDetailHasMany
      }
    ]
  })
  console.log("getAllDict--%s==", JSON.stringify(data))
  return data
}
const getBatchDictDetails = async (store_id, names) => {
  console.log("getAllDict--store_id: %s==", store_id)
  let where = {
    store_id: {
      [Sequelize.Op.in]: ["0", store_id]
    }
  };
  if (names) {
    const nameList = names ? names.split(',') : [];
    console.log("getBatchDictDetails--%s==", JSON.stringify(nameList))
    where.name = { [Sequelize.Op.in]: nameList };
  }
  const data = await Dict.findAll({
    where,
    include: [
      {
        association: DictDetailHasMany,
        
      }
    ]
  })
  console.log("getBatchDictDetails--%s==", JSON.stringify(data))
  return data
}

module.exports = {
  getAllDict,
  getBatchDictDetails
}
